package com.findyou.mapper;

import com.findyou.entity.Occupation;
import com.findyou.entity.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/**
 * @Title: UserMapper
 * @Author 贾金博
 * @Package com.findyou.mapper
 * @Date 2024/11/1 上午10:30
 * @description: 通过注解的方式去执行sql语句
 */

@CacheNamespace
/*开启二级缓存 放在mapper上面*/
public interface UserMapper {

    /**
     * 查询全部的用户 用的是注解的形式
     * @return
     */
    @Select("select * from user")
    List<User> findAll();

    /**
     * 插入注解操作
     * @param user
     */
    @Insert("insert into user (username) values ( #{username} )")
    void insertUser(User user);

    /**
     * 更新注解操作
     * @param user
     */
    @Update("update user set username = #{username} where id = #{id}")
    void updateUser(User user);

    /**
     * 删除注解操作
     * @param user
     */
    @Delete("delete from user where id = #{id}")
    void deleteUser(User user);

    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    @Select("select * from user where id = #{id}")
    User getUserById(int id);


    /**
     * 查询全部用户对应的全部订单
     * @return
     */
    @Select("select * from user")
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "username", property = "username"),
            @Result(property = "ordersList", javaType = List.class,
                column = "id", many = @Many(select = "com.findyou.mapper.OrdersMapper.findOrdersByUid",
                    fetchType = FetchType.EAGER /*结束懒加载*/)
            )
    })
    List<User> findAllWithOrder();

    /**
     * 根据用户查询用户的全部角色
     * 多对多的 一个用户对应多个角色 一个角色对应多个用户
     * @return
     */
    @Select("select * from user")
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "username", property = "username"),
            @Result(property = "occupationList", javaType = List.class,
                    column = "id", many = @Many(select = "com.findyou.mapper.OccupationMapper.findByUid",
                    fetchType = FetchType.EAGER)
            )
    })
    List<User> findAllWithOccupation();
}
